package io.gbznt.modules.app.controller;

import com.gexin.rp.sdk.template.NotificationTemplate;
import io.gbznt.common.utils.R;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.modules.app.entity.UserEntity;
import io.gbznt.modules.app.service.UserService;
import io.gbznt.modules.app.utils.NotificationUtil;
import io.gbznt.modules.sys.entity.SysUserEntity;
import io.gbznt.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/app")
@Api("APP更新设备ID接口")
public class AppUpdateClientIdController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private NotificationUtil notificationUtil;

    @PostMapping("updateClientId")
    public R modifyClientID(@RequestParam String clientid){
        SysUserEntity userEntity = new SysUserEntity();
        userEntity.setId(ShiroUtils.getUserId());
        userEntity.setClientid(clientid);
        sysUserService.updateById(userEntity);
        return R.ok();
    }

    @PostMapping("push")
    @ApiOperation("推送通知")
    public R push(@RequestParam Map<Object,Object> map){
        NotificationTemplate template = notificationUtil.notificationTemplateDemo(map);
        Map<String, Object> map1 = new HashMap<>();
        if (template != null) {
            map1.put("Notification",template);
            return R.ok(map1);
        } else {
            return R.error();
        }
    }
}
